<?php
/**
 * 房屋管理
 * @author Administrator
 */
	class houseControl extends commonControl{
		/**
		 * 显示所有房子
		 */
		public function index(){
			$db = V('house');
			$db->view = array(
					'xiaoqu'=>array(
							'join_type'=>'inner',
							'field'=>'qname,qid',
							'on'=>'house.qid = xiaoqu.qid',
						),
					'citys'=>array(
							'join_type'=>'inner',
							'field'=>'cname',
							'on'=>"house.cid = citys.cid"
						),
				);
			
			$count = $db->field("id,title,addtime,house.cid,house.qid,cname,qname,house.status")->count();
			$page = new page($count,12);
			$houses = $db->field("id,title,addtime,house.cid,house.qid,cname,qname,house.status")->limit($page->limit())->all();
			$this->assign("page",$page->show(2));
			$this->assign("houses",$houses);
			$this->display();
		}
		/**
		 * 增加房屋视图
		 */
		public function add_show(){
			$this->get_city_xiaoqu($_GET);//向模板 分配 当前所在 城市和小区信息
			$this->house_yangshi(); //获取房屋样式  别墅 平层 等
			$this->get_house_sheshi(); //获取所有的房间设施 并且分配到模板
			$this->get_house_fuwu(); //获得所有服务并分配到模板
			$this->get_house_tese(); //获得所有的特色并且放到模板
			$this->get_house_jingguan_view(); //获得所有景观数据
			$this->display();
		}
		/**
		 * 当get参数中存在cid 和qid 时，向模板 分配 当前所在 城市和小区信息
		 */
		public function get_city_xiaoqu(){
			if(isset($_GET['cid']) && isset($_GET['qid'])){ //当get参数中存在cid和qid的时候
				$db = M("citys");
				$city = $db->field("cname,cid")->where(" status = 1")->find($_GET['cid']);
				$xiaoqu = $db->table("xiaoqu")->field("qid,qname")->where(" status = 1")->find($_GET['qid']);
				$jingqu = $db->table('jingqu')->field("jid,jname")->where(" cid = '".$_GET['cid']."'")->where(" status = 1")->all();
				$this->assign("city",$city);
				$this->assign("xiaoqu",$xiaoqu);
				$this->assign("jingqu",$jingqu);
			}else{
				$db = M("citys");
				$citys = $db->field("cname,cid")->all();
				$xiaoqus = $db->table("xiaoqu")->field("qid,qname")->all();
				$this->assign("citys",$citys);
				$this->assign("xiaoqus",$xiaoqus);
			}
		}
		//获得所有景观数据
		public function get_house_jingguan_view(){
			$db = M("house_jingguan");
			$rows = $db->all();
			$this->assign("jingguan",$rows);
		}
		/**
		 * 获取所有的房间设施
		 */
		public function get_house_sheshi(){
			$db = M("house_sheshi");
			$sheshi = $db->all();
			$this->assign("sheshi",$sheshi);
		}
		/**
		 * 获得所有服务并分配到模板
		 */
		public function get_house_fuwu(){
			$db = M("house_fuwu");
			$fuwu = $db->all();
			$this->assign('fuwu', $fuwu);
		}
		/**
		 * 获得所有的特色并且放到模板
		 */
		public function get_house_tese(){
			$db = M('house_tese');
			$rows = $db->all();
			$this->assign("tese",$rows);
		}
		/**
		 * AJAX 动态的获取 小区和 景区信息
		 */
		public function get_xiaoqu_jingqu(){
			C("debug",0);
			$cid = $_POST['cid'];
			$db = M('xiaoqu');
			$xiaoqus = $db->field("qid,qname")->where(" cid = '".$cid."'")->where(" status = 1")->all();
			$jingqus = $db->table('jingqu')->field("jid,jname")->where(" cid = '".$cid."'")->where(" status = 1")->all();
			$companys = $db->table("companys")->field("comid,comname")->where(" status = 1")->where(" cid = '".$cid."'")->all();
			$xiaoquhtml = '';
			foreach($xiaoqus as $k=>$v){ //循环小区信息
				$xiaoquhtml .= "<option value='".$v['qid']."'>".$v['qname']."</option>";
			}
			$jingquhtml = '';
			foreach($jingqus as $k=>$v){
				$jingquhtml .= "<option value='".$v['jid']."'>".$v['jname']."</option>";
			}
			$companyhtml = '';
			foreach($companys as $k=>$v){
				$companyhtml .= "<option value='".$v['comid']."'>".$v['comname']."</option>";
			}
			$result = array();
			$result['xiaoqu'] = $xiaoquhtml;
			$result['jingqu'] = $jingquhtml;
			$result['company'] = $companyhtml;
			$result = ArrEncode($result);
			echo urldecode(json_encode($result));
		}
		/**
		 * 执行房屋增加
		 */
		public function add_exe(){
			$_POST = $this->do_insert_data($_POST);
			$_POST['addtime'] = time();
			$db = K('houseRelative');
			$hid = $db->join("house_sheshi_rel,house_otherserver,house_data,
					house_position,house_fuwu_rel,house_tese_rel")->add();
			$this->cun_yangshi($_POST['house_yangshi_rel'], $hid['house'], $_POST['cid']);
		}
		/**
		 * 将房屋样式存入中间表
		 */
		public function cun_yangshi($yid,$hid,$cid){
			$db = M('house_yangshi_rel');
			$db->where(" hid = '".$hid."'")->del(); //删除中间表中 此房屋的信息
			$db->add(array('hid'=>$hid,'yid'=>$yid,'cid'=>$cid)); //添加房屋信息
		}
		/**
		 * 增加房屋和 修改房屋 的时候 处理房屋""样式 "" post数据为  平层,1   换成数组
		 */
		public function do_yangshi($str){
			$arr = array();
			$arr = explode(',', $str);
			return $arr;
		}
		/**
		 * 获取房屋样式
		 */
		public function house_yangshi(){
			$db= M('house_yangshi');
			$yangshi = $db->all();
			$this->assign('yangshi', $yangshi);
		}
		public function modify_show(){
			$hid = $_GET['hid'];
			$cid = $_GET['cid'];
			$db = K('houseRelative');
			
			$row = $db->join('house_sheshi_rel,house_data,house_tese_rel,house_yangshi_rel,house_fuwu_rel')->where(" id = '".$hid."'")->find();
			$geju = $row['geju'];
			$row['geju'] = explode(',',$row['geju']);
			foreach($row['house_sheshi_rel'] as $k=>$v){
				$row['house_sheshi'][] = $v['hsid'];
			}
			unset($row['house_sheshi_rel']);
			foreach($row['house_fuwu_rel'] as $k=>$v){
				$row['house_fuwu'][] = $v['fwid'];
			}
			unset($row['house_fuwu_rel']);
			foreach($row['house_tese_rel'] as $k=>$v){
				$row['house_tese'][] = $v['tsid'];
			}
			unset($row['house_tese_rel']);
			$this->assign("house",$row);
			$this->get_city_xiaoqu($_GET);//向模板 分配 当前所在 城市和小区信息
			$this->house_yangshi(); //获取房屋样式  别墅 平层 等
			$this->get_house_sheshi(); //获取所有的房间设施 并且分配到模板
			$this->get_house_fuwu(); //获得所有服务并分配到模板
			$this->get_house_tese(); //获得所有的特色并且放到模板
			$this->mo_get_xq_and_jq_server_position($row['qid'], $cid,$hid);
			$this->display();
		}
		public function mo_get_xq_and_jq_server_position($qid,$cid,$hid){
			$db = M("xiaoqu");
			$rows = $db->where(" cid = '".$cid."'")->all();
			$this->assign("xiaoqu",$rows); //分配小区信息
			$jq = $db->table('jingqu')->where(" cid = '".$cid."'")->all();
			$this->assign("jignqu",$jq); //分配景区信息
			$companys = $db->table("companys")->where(" cid = '".$cid."'")->all();
			$this->assign("companys",$companys); //分配公司信息
			$otherserver = $db->table("house_otherserver")->where(" hid = '".$hid."'")->all();
			$this->assign("otherserver",$otherserver); //分配其他服务
			$position = $db->table("house_position")->where(" hid = '".$hid."'")->all();
			$this->assign("position",$position);
		}
		/**
		 * 处理要插入 或者是 要修改的数据
		 * @param unknown_type $_POST
		 * @return unknown
		 */
		public function  do_insert_data($_POST){
			$_POST['geju'] = $_POST['geju'][0].','.$_POST['geju'][1].','.$_POST['geju'][2]; //处理房屋格局
			$yangshi = $this->do_yangshi($_POST['yangshi']); //将表单中的数据重组 样式
			if(empty($_POST['house_sheshi_rel'])){
				$_POST['house_sheshi_rel'] = array(
							'hsid'=>0,
						);
			}
			if(empty($_POST['house_tese_rel'])){
				$_POST['house_tese_rel'] = array(
							'tsid'=>0,
						);
			}
			foreach($_POST['house_sheshi_rel'] as $k=>$v){ //处理房间设施
				$_POST['house_sheshi_rel'][$k]['cid'] = $_POST['cid'];
			}
			$_POST['startDate'] = strtotime($_POST['startDate']); //处理开始时间
			$_POST['endDate'] = strtotime($_POST['endDate']); //处理结束时间
			foreach($_POST['house_otherserver'] as $k=>$v){ //处理其他服务
				$_POST['house_otherserver'][$k]['fwname'] = $v[0];
				$_POST['house_otherserver'][$k]['value'] = $v[1];
				unset($_POST['house_otherserver'][$k][0]);
				unset($_POST['house_otherserver'][$k][1]);
			}
			if(count($_POST['house_tese_rel']) >4){
				$tese_data = array_rand($_POST['house_tese_rel'],4);
			}else{
				$tese_data = $_POST['house_tese_rel'];
			}
			foreach($_POST['house_tese_rel'] as $k=>$v){
				$_POST['house_tese_rel'][$k]['cid'] = $_POST['cid'];
			}
			foreach($tese_data as $k=>$v){
				$tese_data1[] = $v['tsid'];
			}
			if(empty($_POST['house_youhui'][1]['value'])){
				$_POST['youhui'] = 0;
			}else{
				$_POST['youhui'] = 1;
			}
			$_POST['tese'] = implode(',',$tese_data); //组合 4个特色到house表
			if(!empty($_POST['thumb11'])){
				$_POST['thumb'] = $_POST['thumb11'][1][1]; //选取裁切好的图片
				@unlink(PATH_ROOT.'/'.$_POST['thumb11'][1][0]); //删除冗余图片
				unset($_POST['thumb11']);
			}
			if(!empty($_POST['house_img'])){
				foreach($_POST['house_img'] as $k=>$v){
					$images[] = $v['0'];
					$images_m[] = $v['1'];
				}
				$_POST['house_data']['images'] = var_export($images,true);
				$_POST['house_data']['images_m'] = var_export($images_m,true);
			}
			$_POST['updatetime'] = time();
			$_POST['yangshi'] = $yangshi[0]; //平层 汉字 等
			$_POST['house_yangshi_rel'] = $yangshi[1]; //房屋样式 数字 存入中间表
			return $_POST;
		}
		/**
		 * 执行房屋修改
		 */
		public function modify_exe(){
			$_POST = $this->do_insert_data($_POST);
			$db = K('houseRelative');
			$hid = $db->join("house_sheshi_rel,house_otherserver,house_data,
					house_position,house_fuwu_rel,house_tese_rel")->update($_POST);
			$this->cun_yangshi($_POST['house_yangshi_rel'], $_POST['id'], $_POST['cid']);
		}
		/**
		 * 修改房屋状态(是否显示)
		 */
		public function status(){
			$db = M("house");
			$_GET['status'] = $_GET['set'];
			unset($_GET['set']);
			if($db->where(" id = '".$_GET['hid']."'")->update(array('status'=>$_GET['status']))){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}

?>